<?php
//-------------------------------------------------------------------------------------------------
    include_once($config['app_dir'] . '/includes/object.class.php');
    //---------------------------------------------------------------------------------------------
    class objColumn extends objObject implements  iDebug{
            public $name;
            public $caption;
            public $visible;
            public $editable=false;
            public $align;
            public $type;
            public $valuelist;
            public $flag_key = false;

        function __construct($name, $caption=false, $flag_key=false, $visible=true, $align='left', $type='C', $valuelist=false, $editable=false) {
            $this->name = $name;
            if ($caption) $this->caption = $caption;
                else $this->caption = $name;
            $this->visible = $visible;
            $this->editable = $editable;
            $this->align = $align;
            $this->type = $type;
            $this->flag_key = $flag_key;
            $this->valuelist = $valuelist;
        } //costructor

        public function ClassName() {
            return  __CLASS__;
        }//ClassName

        function getAssocArr() {
            $result = array();
            $result['name'] = $this->name;
            $result['caption'] = $this->caption;
            $result['visible'] = $this->visible;
            $result['editable'] = $this->editable;
            $result['align'] = $this->align;
            $result['type'] = $this->type;
            $result['list'] = $this->valuelist;
            return $result;
        }//getAssocArr

        function dump() {
           echo '<br> Caption=';var_dump($this->caption);
           echo '<br> Name=';var_dump($this->name);
           echo '<br> Type=';var_dump($this->type);
           echo '<br> Visible=';var_dump($this->visible);
           echo '<br> Editable=';var_dump($this->editable);
           echo '<br> FlagKey';var_dump($this->flag_key);
           echo '<br> Align=';var_dump($this->align);
           echo '<br> List <pre>';
           print_r($this->valuelist);
           echo '</pre><br>';
        }//dump


    }//class objPagerColumn

    class objColumnSet extends objObject implements iDebug, Iterator, ArrayAccess{
            protected $data;
            protected $column_count=0;
            protected $id;
            protected $valid;
            protected $_keys=array();
            protected $_name2index_arr=array();
            public $title;

        function __construct($id) {
            $this->data = Array();
            $this->id = $id;
        }//class constructor objColumnSet

		public function ClassName() {
            return  __CLASS__;
        }//ClassName

        function __set($name, $value) {
            switch ($name) {
                case 'visible': foreach ( $this->data as $column  )  $column->visible = $value;
                break;
           } //switch
        } //__set

        function getID() {
            return $this->id;
        } //getID

        function addColumn($column) {
            $this->data[]=$column;
            $this->_name2index_arr[$column->name] = $this->column_count;
            $this->column_count++;
            if ( $column->flag_key ) $this ->_keys[] = $column->name;
        } //addField

        function Column($name) {
            if (array_key_exists($name, $this->_name2index_arr) ) {
                return $this->data[intval($this->_name2index_arr[$name])];
            }
            return false;
        } //Column

        function ColumnNo($nindex) {
            return $this->data[$nindex];
        } //ColumnNo

        function length() {
            return $this->column_count;
        } //length

        function getData(){
            return $this->data;
        } //getData

        function setTypes($types) {
            foreach ( $types as $name => $type  )
                if ($column = $this->Column($name) ) $column->type = $type;
        }//setTypes

        function getKeys($names=false) {
            $result = array();
            foreach($this ->_keys as $name ) {

                if (false === $names ||  in_array($name, $names) )
                    $result[]= $this->Column($name);
            }
            return $result;
        } //getKeys

        function getFirstKey() {
            $result = false;
            if (is_array($this ->_keys)&& count($this ->_keys)>0)  $result =  $this ->_keys[0];
            return $result;
        } //getFirstKey

        function getNameKeys($names=false) {
            if (false === $names)     return $this ->_keys;
                else return array_intersect($this ->_keys, $names);
        } //getNameKeys

        function getNameColumns($names=false) {
            $result = array();
            foreach ( $this->data as $tmp_clmn  )
                if (false === $names ||  in_array($tmp_clmn->name, $names) )
                    $result[] = $tmp_clmn->name;
            return $result;
        } //getArrColumns

        function getEditabelNames() {
            $result = array();
            foreach ( $this->data as $column  )
                if( $column->editable )
                    $result[] = $column->name;
            return $result;
        }//getEditableNames

        function getNameByType($need_type, $names=false) {
            $result = array();

            $needle = array();
            if ( is_array($need_type) ) $needle = $need_type;
                elseif( !empty($need_type) ) $needle[] = $need_type;

            foreach ( $this->data as $tmp_clmn  )
                if ( in_array($tmp_clmn->type, $needle) && (false === $names ||  in_array($tmp_clmn->name, $names)))
                    $result[] = $tmp_clmn->name;
            return $result;
        } //getArrColumns

        function getAssocArr($names=false) {
            $result = array();
            foreach ( $this->data as $column  )
                if ($names===false || in_array($column->name, $names) )
                    $result[] = $column->getAssocArr();
            return $result;
        }//getAssocArr

        function countVisible() {
            $cnt=0;
            foreach ( $this->data as $tmp_fld  )
                if ($tmp_fld->visible) $cnt++;
            return $cnt;
        } //countVisible

        function  setVisibleArray($va, $hide_all=false) {
            if ($hide_all) $this->visible = 0;
            foreach($va as $name => $value) {
                if ( $column = $this->Column($name) ) $column->visible= $value;
            } //foreach
        } //setVisibleArray

        function getVisibleArray() {
            $result = array();
            foreach ($this->data as $field)         {
                $result[$field->name] = $field->visible;
            }
            return $result;
        } //getVisibleArray

        function paramsVisibleFields() {
            $i = 0;
            $visible_fileds = array();
            foreach($this->data as $column)
                if ($column->visible) {
                    $visible_fileds[$i]['value']  = $column->name;
                    $visible_fileds[$i++]['name'] = $column->caption;
                }
            return $visible_fileds;
        }//paramsVisibleFields()

        function dump() {
            foreach($this->data as $tmp_fld) {
               $tmp_fld ->dump();
            } //foreach
        } //dump

        //--------------------------------------------------------------------------------------------
        // Iterator
        //--------------------------------------------------------------------------------------------

        function rewind() {
            $this->valid = (FALSE !== reset($this->data));
        } //rewind

        function current() {
            return current($this->data);
        } //current

        function key() {
            return key($this->data);
        } //key

        function next() {
            $this->valid = (FALSE!==next($this->data));
        } //next

        function valid() {
            return $this->valid;
        } //valid

        //--------------------------------------------------------------------------------------------
        // ArrayAccess
        //--------------------------------------------------------------------------------------------

        function offsetSet($key, $value) {
            if (array_key_exists($key, $this->_name2index_arr) ) {
                $this->data[intval($this->_name2index_arr[$key])] = $value;
            }
        } //offsetSet

        function offsetGet($key) {
            if (array_key_exists($key, $this->_name2index_arr) ) {
                return $this->data[intval($this->_name2index_arr[$key])];                        }
        } //offsetGet

        function offsetUnset($key) {
            if (array_key_exists($key, $this->_name2index_arr) ) {
                unset($this->data[intval($this->_name2index_arr[$key])]);
            }
        } //offsetUnset

        function offsetExists($key) {
            return array_key_exists($key, $this->_name2index_arr);
        } //offsetExists

    } //      class objColumnSet
//-------------------------------------------------------------------------------------------------
?>
